package org.snaker.engine.parser.logicflow.node;


import org.snaker.engine.model.FieldModel;
import org.snaker.engine.model.NodeModel;
import org.snaker.engine.model.TaskModel;
import org.snaker.engine.parser.logicflow.LogicFlowJSONAbstractNodeParser;
import org.snaker.engine.parser.logicflow.model.LogicFlowData;

import java.util.ArrayList;
import java.util.List;
import java.util.Objects;

/**
 *
 * 任务节点解析类
 * @author mldong
 * @date 2023/4/26
 */
public class TaskParser extends LogicFlowJSONAbstractNodeParser {

    /**
     * 解析task节点特有属性
     * @param node
     */
    @Override
    public void parseNode(NodeModel model, LogicFlowData.Node node) {
        TaskModel taskModel = (TaskModel)nodeModel;
        LogicFlowData.Properties properties = node.getProperties();
        taskModel.setForm(properties.get(ATTR_FORM));
        taskModel.setAssignee(properties.get(ATTR_ASSIGNEE));
        taskModel.setAssignmentHandler(properties.get(ATTR_ASSIGNEE_HANDLER));
        taskModel.setTaskType(properties.get(ATTR_TASKTYPE));
        taskModel.setPerformType(properties.get(ATTR_PERFORMTYPE));
        taskModel.setReminderTime(properties.get(ATTR_REMINDERTIME));
        taskModel.setReminderRepeat(properties.get(ATTR_REMINDERREPEAT));
        taskModel.setExpireTime(properties.get(ATTR_EXPIRETIME));
        taskModel.setAutoExecute(properties.get(ATTR_AUTOEXECUTE));
        taskModel.setCallback(properties.get(ATTR_CALLBACK));

        List<FieldModel> fields = new ArrayList<FieldModel>();
        // 自定义扩展属性
        LogicFlowData.Properties fieldsProperties = properties.getProperties(ATTR_FIELD);
        if(Objects.nonNull(fieldsProperties)) {
            fieldsProperties.forEach((k,v)->{
                FieldModel fieldModel = new FieldModel();
                fieldModel.setName(k);
                fieldModel.setDisplayName(k);
                //TODO 暂定全部为字符串
                fieldModel.setType("String");
                fields.add(fieldModel);
            });
        }

        taskModel.setFields(fields);
    }

    @Override
    public NodeModel newModel() {
        return new TaskModel();
    }
}
